$(window).load(function() {


	var url = window.location.hash.substring(2);
	var params = "";
	var show = "";
	var parameterStart = url.indexOf("layout");
	if(parameterStart != -1){
		// calendar append parameters if any
		params = {layout: url.substring(parameterStart+7)};
		show = "/layout/"+params.layout;
		url = "calculator";
		
	}
	var parameterStart = url.indexOf("spec");	
	if(parameterStart != -1){
		// calendar append parameters if any
		params = {spec: url.substring(parameterStart+5)};
		show = "/spec/"+params.spec;
		url = "calculator";
	}
	// Whe coming from a new browser window
	
	var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
	//alert(hashes[1])
	
	//var urlVars = getUrlVars();
	var params = {};
	if(hashes[1] != undefined){
		url = "calculator";
		params = { spec: hashes[1] }
	}else{
		url = "browse";;
	}
	if(hashes[2] != undefined){
		url = "xivspecs";
		params = { spec: hashes[1] }
	}
	
	loadURL(url, params);
});

$(document).ready(function(){
	$('.specs .menu-item').click(function(e) {
		var clicked = jQuery(e.target);
		var url = clicked.text().toLowerCase();
		loadURL(url, {});
	});
});

function loadURL(url, params){

	colorMenu(url);
	if(url == "create"){
		url = "calculator";
	}
	
	if(url=="1.20"){
		url= "xivspecs";
	}
	
	$("#left-content-container").html("<div style='margin-top:200px;' class=\"center loader-gif\"></div>")
	$("#left-content-container").load("./specs/"+url+".php", addSessionVars(params), function(response, status, xhr) {
		$("#left-content-container").hide();
		$("#left-content-container").fadeIn('fast');
		if (status == "error") {
			var msg = "Sorry but there was an error: ";
			$("#left-content-container").html(msg + xhr.status + " " + xhr.statusText);
		}
	});

};

function getUrlVars(){
	var vars = [], hash;
	
	for(var i = 0; i < hashes.length; i++)
	{
		hash = hashes[i].split('=');
		vars.push(hash[0]);
		vars[hash[0]] = hash[1];
	}
	return vars;
};

function colorMenu(url){
	$('.specs .menu-item').each(function(index) {
		if($(this).text().toLowerCase() == url){
			$(this).addClass("menu-item-selected");
		}else{
			$(this).removeClass("menu-item-selected");
		}
	});
};

function loadOwnSpecs(){
	$("#own-specs").fadeIn('fast');
	$("#own-specs-spacer").fadeIn('fast');
	$("#own-specs-container").html("<div style='margin-top:0px;' class=\"center loader-gif\"></div>")
	$("#own-specs-container").load("./specs/widget_own_specs.php", addSessionVars({}), function(response, status, xhr) {
		$("#own-specs-container").fadeIn('fast');
		if (status == "error") {
			var msg = "Please login to see your specs";
			$("#own-specs-container").html(msg + xhr.status + " " + xhr.statusText);
		}
	});
};

function loadHotSpecs(){
	$("#hot-specs-container").html("<div style='margin-top:0px;' class=\"center loader-gif\"></div>")
	$("#hot-specs-container").load("./specs/widget_hot_specs.php", addSessionVars({}), function(response, status, xhr) {
		$("#hot-specs-container").fadeIn('fast');
		if (status == "error") {
			var msg = "Please login to see your specs";
			$("#hot-specs-container").html(msg + xhr.status + " " + xhr.statusText);
		}
	});
};

function loadInfoSpecs(sId){

	if(sId != undefined && sId != ""){
		$("#info-specs").show();
		$("#info-specs-spacer").fadeIn('fast');
		$("#info-specs-container").html("<div style='margin-top:0px;' class=\"center loader-gif\"></div>")
		$("#info-specs-container").load("./specs/widget_spec_detail.php", addSessionVars({specId: sId}), function(response, status, xhr) {
			$("#info-specs-container").fadeIn('fast');
			if (status == "error") {
				var msg = "Please login to see your specs";
				$("#info-specs-container").html(msg + xhr.status + " " + xhr.statusText);
			}
		});
	}else{
		$("#info-specs-container").hide();
	}
};

function addSessionVars(params){

	//params['CICUID'] = CICUID;
	params['THEME'] = THEME;
	params['AVATAR'] = AVATAR;
	return params;
}
function addPadSessionVars(params){

	params['CICUID'] = CICUID;
	params['THEME'] = THEME;
	params['AVATAR'] = AVATAR;
	
	return params;

}
